package com.gubatian.jui.demo;

/**
 * 剖析interrupted原理
 * @author gubatian
 * @date 2021/9/8 2:12 下午
 */
public class InterruptedDemo {
    public static void main(String[] args) throws InterruptedException {
        InterruptedThread interruptedThread = new InterruptedThread();
        interruptedThread.start();

        Thread.sleep(1000);
        interruptedThread.setRunning(false);
        interruptedThread.interrupt();
    }

    private static class InterruptedThread extends Thread {
        private Boolean shouldRun = true;
        @Override
        public void run() {
            while (shouldRun) {
                try {
                    System.out.println("线程未中断");
                    Thread.sleep(30 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void setRunning(boolean shouldRun) {
            this.shouldRun = shouldRun;
        }
    }
}
